<?php

namespace App\Http\Controllers\Admin;

use Cartalyst\Sentry\Facades\Laravel\Sentry;
use App\Http\Model\Category;
use App\Http\Model\Auth;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Validator;

class CategoryController extends CommonController
{
	//post 根据权限获取菜单
    public function top(){
		if($a_id=Route::Input('id')){
	    	$user = Sentry::getUser();

			if($user->id==1){
				$auth=Auth::where(['showed'=>1,'activated'=>1])->orderBy('orders','asc')->get();
				$cates=catTree($auth,'a_id',$a_id);
				$d=[];
				$num=0;
				foreach($cates as $v){
					if($v->child){
						$d[$num]['name']=$v->name;
						$n=0;
						foreach($v->child as $r){
							$rid=str_replace("admin.","",$r->role);
							$rid=str_replace(".","-",$rid);
							$d[$num]['children'][$n]['id']=$rid;
							$d[$num]['children'][$n]['name']=$r->name;
							$d[$num]['children'][$n]['target']="navtab";
							$d[$num]['children'][$n]['url']=str_replace(".","/",$r->role);
							$n++;
						}
					}else{
						$rid=str_replace("admin.","",$v->role);
						$rid=str_replace(".","-",$rid);
						$d[$num]['id']=$rid;
						$d[$num]['name']=$v->name;
						$d[$num]['target']="navtab";
						$d[$num]['url']=str_replace(".","/",$v->role);
					}
					$num++;
				}
				return $d;
			}else{
				// 获得合并后的权限信息
				$permissions = $user->getMergedPermissions();
				foreach($permissions as $k=>$v){
					if($v==1){
						$key[]=$k;
					}
				}
				$auth=Auth::where(['showed'=>1,'activated'=>1])->orderBy('orders','asc')->get();
				$cate=catTree($auth,'a_id',$a_id);
				$cates=[];
				foreach($cate as $r){
					if(in_array($r->role,$key)){
						$cates[]=$r;
					}
				}
				$d=[];
				$num=0;
				foreach($cates as $v){
					if($v->child){
						$d[$num]['name']=$v->name;
						$n=0;
						foreach($v->child as $r){
							$rid=str_replace("admin.","",$r->role);
							$rid=str_replace(".","-",$rid);
							$d[$num]['children'][$n]['id']=$rid;
							$d[$num]['children'][$n]['name']=$r->name;
							$d[$num]['children'][$n]['target']="navtab";
							$d[$num]['children'][$n]['url']=str_replace(".","/",$r->role);
							$n++;
						}
					}else{
						$rid=str_replace("admin.","",$v->role);
						$rid=str_replace(".","-",$rid);
						$d[$num]['id']=$rid;
						$d[$num]['name']=$v->name;
						$d[$num]['target']="navtab";
						$d[$num]['url']=str_replace(".","/",$v->role);
					}
					$num++;
				}
				return $d;
			}
		}
		return '';
    }
	
    //get admin/category 全部分类列表
    public function index(){
        $cates=Category::orderBy('orders','asc')->get();
        $data=catlist($cates,'pid',$catename='_cate_name');
        return view('admin.category.index')->with('data',$data);
    }

    //get admin/category/create 添加分类
    public function create(){
        $cates=Category::orderBy('orders','asc')->get();
        $data=catlist($cates,'pid',$catename='_cate_name');
        return view('admin.category.add')->with('data',$data);
    }

    //post admin/category
    public function store(){
        $input = Input::except('_token');
        $rules = [
            'name'=>'required',
            'orders'=>'numeric'
        ];
        $message = [
            'name.required'=>'分类名称必须填写！',
            'orders.numeric'=>'排序必须是数字！',
        ];
        $validator = Validator::make($input,$rules,$message);

        if($validator->passes()){
            $re = Category::create($input);
            if($re){
                return redirect('admin/category');
            }else{
                return back()->with('errors','数据填充失败，请稍后重试！');
            }
        }else{
            return back()->withErrors($validator);
        }
    }

    //get admin/category/{category}  显示单个分类信息
    public function show(){

    }

    //put admin/category/{category}  更新分类
    public function update($cate_id){
        $input = Input::except('_token','_method');
        $rules = [
            'name'=>'required',
            'orders'=>'numeric'
        ];
        $message = [
            'name.required'=>'分类名称必须填写！',
            'orders.numeric'=>'排序必须是数字！',
        ];
        $validator = Validator::make($input,$rules,$message);
        if($validator->passes()){
            $re = Category::where('id',$cate_id)->update($input);
            if($re){
                return redirect('admin/category');
            }else{
                return back()->with('errors','分类信息更新失败，请稍后重试！');
            }
        }else{
            return back()->withErrors($validator);
        }
    }

    //DELETE admin/category/{category}  删除单个分类
    public function destroy($cate_id){
        $re = Category::where('id',$cate_id)->delete();
        Category::where('pid',$cate_id)->update(['pid'=>0]);
        if($re){
            $data = [
                'status' => 200,
                'msg' => '分类删除成功！',
            ];
        }else{
            $data = [
                'status' => 300,
                'msg' => '分类删除失败，请稍后重试！',
            ];
        }
        return $data;
    }

    //GET admin/category/{category}/edit  编辑分类
    public function edit($cate_id){
        $field = Category::find($cate_id);
        $cates=Category::orderBy('orders','asc')->get();
        $data=catlist($cates,'pid',$catename='_cate_name');

        $ids=getChildsId($cates,$cate_id,'pid');
        foreach($data as $v){
            if(in_array($v->id,$ids) || $v->id==$cate_id){
                $v['disabled']=1;
            }else{
                $v['disabled']=0;
            }
        }
        return view('admin.category.edit',compact('field','data'));
    }

    //changeOrder
    public function changeOrder(){
        $input=Input::all();
        $cate=Category::find($input['id']);
        $cate->orders=$input['orders'];
        $re=$cate->update();
        if($re){
            $data=[
                'status' => 200,
                'msg' => '分类排序更新成功！',
            ];
        }else{
            $data=[
                'status' => 300,
                'msg' => '分类排序更新失败！',
            ];
        }
        return $data;
    }
}
